import NProgress from 'nprogress'; // progress bar
import type { WhiteNameList } from './constant';
import type { Router, RouteLocationNormalized } from 'vue-router';


NProgress.configure({ showSpinner: false }); // NProgress Configuration

const defaultRoutePath = '/index';




export function createRouterGuards(router: Router, whiteNameList: WhiteNameList) {
  router.beforeEach(async (to, from, next) => {

    if (!from.meta?.hideProgressBar || !to.meta?.hideProgressBar) {
      NProgress.start(); // start progress bar
    }
    console.log(to, 'to')
    document.title = to.meta.title
    if (to.query?.ticket!=undefined){
      localStorage.setItem('ticket',to.query?.ticket);
    }


     next();
  });

  /** 获取路由对应的组件名称 */
  const getComponentName = (route: RouteLocationNormalized): string[] => {
    return route.matched
      .map((n) => {
        if (!n.meta?.keepAlive) return;
        const comp = n.components?.default;
        return comp?.name ?? (comp as any)?.type?.name;
      })
      .filter(Boolean);
  };

  router.afterEach((to, from, failure) => {
    NProgress.done(); // finish progress bar
  });

  router.onError((error) => {
    console.error('路由错误', error);
  });
}
